package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.ex.ShopLoginException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.ShopLoginService;
import em.ResuleEnum;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("shoplogin")
public class ShopLoginController {

    @DubboReference
    private ShopLoginService shopLoginService;

    @RequestMapping("toshoplogin")
    public String toshoplogin(){
        return "shoplogin";
    }

    @RequestMapping("toregistee")
    public String toregistee(){
        return "register";
    }

    @RequestMapping("denglu")
    @ResponseBody
    public AjaxResult denglu(TbSeller tbSeller){
        //创建subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户的账号密码 token对象
        UsernamePasswordToken token = new UsernamePasswordToken(tbSeller.getSellerId(),tbSeller.getPassword());
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return AjaxResult.R(ResuleEnum.UNKNOWNACCOUNT_ERROR);
        }catch (IncorrectCredentialsException e1){
            return AjaxResult.R(ResuleEnum.INCORRECT_ERROR);
        }catch (ShopLoginException e2){
            return AjaxResult.R(ResuleEnum.SHENHENO_ERROR);
        }
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }

    @RequestMapping("zhuce")
    @ResponseBody
    public AjaxResult zhuce(TbSeller tbSeller){
        shopLoginService.zhuce(tbSeller);
        return AjaxResult.ok();
    }


}
